<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta username="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      "use strict";
      const user = {
        username: "jack",
        // 两个方法合并一个方法
        login(flag) {
          if (flag === true) {
            console.log(`${this.username} logged in`);
          } else {
            console.log(`${this.username} failed to log in`);
          }
        },
      };
      function askPassword(ok, fail) {
        const password = prompt("password?");
        if (password === "123123") {
          //ok =  user.loginOk;
          ok(); //丢失this
        } else {
          // fail = user.loginFail;
          fail(); //丢失this
        }
      }
      // user.login.bind(user, true)
      // user.login.bind(user, false)
      askPassword(user.login.bind(user, true), user.login.bind(user, false));
    </script>
  </body>
</html>
